package com.yang.Hash_;

import java.util.HashSet;
import java.util.Set;

public class LeetCode349 {
    public static void main(String[] args) {
        SolutionLeetCode349 solutionLeetCode349 = new SolutionLeetCode349();
        int[] nun1 = {12,1,2,3,4,9,9,8,5};
        int[] nun2 = {1,3,5,9,88,5,4};
        int[] intersection = solutionLeetCode349.intersection(nun1, nun2);
        for (Integer integer : intersection) {
            System.out.println(integer);
        }

    }
}


class SolutionLeetCode349 {
    public int[] intersection(int[] nums1, int[] nums2) {

        // 使用泛型是因为下面要用增强for循环要确定好列表的数据类型
        Set<Integer> set = new HashSet<Integer>();
        Set<Integer> reSet = new HashSet<Integer>();

        for (int i : nums1){
            set.add(i);
        }
        for (int i : nums2){
            if(set.contains(i)){
                reSet.add(i);
            }
        }
        int[] result = new int[reSet.size()];
        int index = 0;
        for( Integer i : reSet){
            result[index++] = i;
        }
        return result;
    }
}
